/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.enrstu.service.impl;

import cn.hutool.core.util.IdUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.enrstu.domain.ReviewInformationLog;
import me.zhengjie.modules.enrstu.repository.ReviewInformationLogRepository;
import me.zhengjie.modules.enrstu.service.ReviewInformationLogService;
import me.zhengjie.modules.enrstu.service.mapstruct.ReviewInformationLogMapper;
import me.zhengjie.utils.ValidationUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author yyx
* @date 2024-11-08
**/
@Service
@RequiredArgsConstructor
public class ReviewInformationLogServiceImpl implements ReviewInformationLogService {

    private final ReviewInformationLogRepository reviewInformationLogRepository;
    private final ReviewInformationLogMapper reviewInformationLogMapper;



    @Override
    @Transactional(rollbackFor = Exception.class)
    public String create(ReviewInformationLog resources) {
        resources.setId(IdUtil.simpleUUID());
        ReviewInformationLog save = reviewInformationLogRepository.save(resources);
        return save.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ReviewInformationLog resources) {
        ReviewInformationLog reviewInformationLog = reviewInformationLogRepository.findById(resources.getId()).orElseGet(ReviewInformationLog::new);
        ValidationUtil.isNull( reviewInformationLog.getId(),"ReviewInformationLog","id",resources.getId());
        reviewInformationLog.copy(resources);
        reviewInformationLogRepository.save(reviewInformationLog);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<ReviewInformationLog> findByChildId(String ChildrenId) {
        return reviewInformationLogRepository.findByReviewId(ChildrenId).stream().map(f->{
            if (f.getReviewComments() == null || f.getReviewResult() == null ||
                    f.getReviewPersonId() == null  || f.getReviewStage() == null || f.getReviewStatus() == null) {
                f.setReviewComments("");
            }

            return f;
        }).collect(Collectors.toList() );
    }

}